<template>
  <div class="myContailer">
    <el-row class="myTitleBorderBottom">
      <el-col :span="20" class="myLineHeight_40px">
        <span v-if="type=='add'">新增小区</span>
        <span v-if="type=='update'">编辑小区</span>
        <span v-if="type=='info'">小区详情</span>
      </el-col>
    </el-row>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          <span class="require myVA_M">*</span> 所属片区 :
        </el-col>
        <el-col :span="7">
          <el-form-item prop="district_selected">
            <el-select v-model="ruleForm.district_selected"
              :loading="loading"
              :disabled="is_disabled"
              placeholder="请选择片区" >
              <el-option
                v-for="item in district_list"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          <span class="require myVA_M">*</span> 小区名称 :
        </el-col>
        <el-col :span="7">
          <el-form-item prop="area_name">
            <el-input :readonly="is_modifly" v-model="ruleForm.area_name" placeholder="请输入小区名称"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          <span class="require myVA_M">*</span> 小区地址 :
        </el-col>
        <el-col :span="7">
          <el-form-item prop="area_address">
            <el-input :readonly="is_modifly" v-model="ruleForm.area_address" placeholder="请输入小区地址"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          小区编号 :
        </el-col>
        <el-col :span="7">
          <el-input v-model="ruleForm.area_no" :readonly="is_modifly" placeholder="请输入小区编号"></el-input>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          占地面积 :
        </el-col>
        <el-col :span="7">
          <el-input v-model="ruleForm.floor_space" :readonly="is_modifly" placeholder="请输入占地面积 ㎡"></el-input>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          总建筑面积 :
        </el-col>
        <el-col :span="7">
          <el-input :readonly="is_modifly" v-model="ruleForm.total_floor_space" placeholder="请输入总建筑面积 ㎡"></el-input>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          绿化率 :
        </el-col>
        <el-col :span="7">
          <el-input :readonly="is_modifly" v-model="ruleForm.greening_ratio" placeholder="请输入绿化率 %"></el-input>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          备注 :
        </el-col>
        <el-col :span="7">
          <el-input type="textarea" v-model="ruleForm.extra_desc" :readonly="is_modifly" placeholder="备注"></el-input>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="10" :offset="3">
          <el-button v-if="type=='add'" @click="create('ruleForm')" class="myBgGreen">新 增</el-button>
          <el-button v-if="type=='update' && isShow(112)" @click="modify('ruleForm')" class="myBgGreen">保 存</el-button>
          <el-button @click="onBack" class="myBgGreen">返 回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<style scoped>
  .myContailer{
    background: #fff;
    padding: 10px;
  }
  .myTitleBorderBottom{
    color: #000;
    font-weight: 500;
    font-size: 16px;
    border-bottom: 1px solid #dbdbdb;
  }
  .myBgGreen{
    background: #00c2a9;
    border-color: #00c2a9;
    color: #fff;
    margin-top:2px;
  }
  .myBgGreen:hover{
    background: #33ceba;
  }
  .myTextAlignRight{
    text-align: right;
  }
  .myLineHeight_40px{
    line-height: 40px;
  }
  .myMarginTop_10px{
    margin-top: 10px;
  }
  .myVA_M{
    vertical-align: middle;
  }

</style>
<script>
  import { getAreaDetail, getDistrictList, createArea, modifyArea } from '@/api/area';

  export default {   
    data(){
      return{
        title:"小区管理",
        
        id: null,
        district_list:[],
        district_loaded: false,
        loading:false,

        type:'',
        is_modifly:true,
        is_disabled:false,
        ruleForm:{
          district_selected:''
        },
        rules: {
          district_selected: [
            { required: true, message: '请选择所属片区', trigger: 'change' }
          ],
          area_name:[
            {required: true, message: '请输入小区名称', trigger: 'blur'}
          ],
          area_address:[
            {required: true, message: '请输入小区地址', trigger: 'blur'}
          ]
        }
      }
    },
    mounted(){
      var vm = this;
      vm.type = vm.$route.query.type;
      vm.id = vm.$route.query.id;
      vm.onGetDistrictList();//拉取片区

      if (vm.type == 'info') { //查看
        vm.is_modifly = true;
        vm.is_disabled = true;
        vm.fetchDetail();
      }

      if(vm.type=='update'){ //修改
        vm.is_modifly = false;
        vm.fetchDetail();
      }

      if(vm.type=='add'){
        vm.is_modifly = false;
      }

      
    },

    methods:{     
      fetchDetail() {
        let vm = this;
        vm.loading = true;
        getAreaDetail(vm.id)
        .then(response => {
          let res=response.data;
          vm.ruleForm=Object.assign(vm.ruleForm, res)
          vm.ruleForm.district_selected=res.district_info.value
          // console.log("拉取的小区详细",vm.ruleForm)
          vm.loading = false;
        })
        .catch(error => {
          vm.loading = false;
        })
      }, 
      onGetDistrictList(){
        let vm=this;
        getDistrictList()
        .then(response => {
          vm.district_list=response.data;
        })
        .catch(error => {
          // console.log("拉取失败",error)
        })
      },
      modify(formName){//编辑
        let vm = this;
        vm.$refs[formName].validate((valid) => {//验证
          if (valid) {
            vm.ruleForm.district_id = vm.ruleForm.district_selected;
            vm.loading = true;
            modifyArea(vm.id, vm.ruleForm)
            .then(response => {
              vm.$message.success("执行成功！");
              vm.loading = false;
              vm.onBack();
            })
            .catch(error=> {
              vm.loading = false;
            });
          } else {
            vm.$message.error("执行失败！");
            return false;
          }
        });
      },
      create(formName){//新增
        let vm = this;
        vm.$refs[formName].validate((valid) => {//验证
          if (valid) {
            vm.ruleForm.district_id = vm.ruleForm.district_selected;
            vm.loading = true;
            createArea(vm.ruleForm)
            .then(response => {
              vm.$message.success("执行成功！");
              vm.loading = false;
              vm.onBack();
            })
            .catch(error=> {
              vm.loading = false;
            });
          } else {
            vm.$message.error("执行失败！");
            return false;
          }
        });        
      },
      onBack(){//返回
        let vm=this;
        window.localStorage.setItem('pageNum',vm.$route.query.pageNum||1);
        vm.$router.go(-1);
      }
    }
  
  }


</script>



